﻿var currentPage = 1; // 当前页
var pageSize = 50; // 每页显示多少条
var pageall = 0; // 总页数
var total = 0; // 总记录数

// 读取列表
$(function() {
    showList(currentPage);
});

// 条件查询资源列表
function showList(currentPage) {
    var customerName = $("#iCustomer").val();               // 客户名称
    var customerTel = $("#iCustomerTel").val();             // 客户电话    
    var room = $("#sRoom").val();                           // 房
    var hall = $("#sHall").val();                           // 厅
    var acreage1 = $("#iAcreage1").val();                   // 面积
    var acreage2 = $("#iAcreage2").val();                   // 面积
    var price1 = $("#iPrice1").val();                       // 租售价
    var price2 = $("#iPrice2").val();                       // 租售价
    var tradeType = $("#sTradeType").val();                 // 交易类型 如:求购、求租

    // 高级查询
//    var id = $("#iID").val();                               // 序号            
//    var fitment = LexunJS.GetSelectValue("sFitment");       // 装修
//    var floor = $("#iFloor").val();                         // 楼层
//    var addDate1 = $("#iStartDay").val();                   // 委托日期
//    var addDate2 = $("#iEndDay").val();                     // 委托日期
//    var usage = $("#sUsage").val();                         // 用途
//    var houseStyle = LexunJS.GetSelectValue("sHouseStyle"); // 样式
//    var direct = LexunJS.GetSelectValue("sDirect");         // 朝向
//    var type = LexunJS.GetSelectValue("sType");            // 类型
//    var isSelf = LexunJS.GetSelectValue("sIsSelf");         // 性质 -1全部 0私客 1公客                          

    $("#divList").html("<img src=\"../images/loading.gif\" />"); // 清空列表 
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=customer_list_action&typeid=2&page=' + currentPage + '&pagesize=' + pageSize + '&customerName=' + customerName + '&customertel=' + customerTel
                     + '&room=' + room + '&hall=' + hall + '&acreage1=' + acreage1 + '&acreage2=' + acreage2
                    + '&price1=' + price1 + '&price2=' + price2 + '&tradeType=' + tradeType,  
        timeout: '20000',
        error: function() {
            alert("系统繁忙,请稍候后试！");
        }, success: function(json) {
            listCallBack(json);
        }
    });
}

/*
功能: 将返回的json数据进行排版
json: json数据
*/

function listCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) {
        $("#divList").html("<font color='red'>&nbsp;系统内暂无您查询的信息!</font>");
        return;
    }

    var page = json.page;

    currentPage = page.currentpage;
    pageall = parseInt(page.pageall);
    total = page.total;
    //用户ID
    var userid = document.getElementById("UserId").value
    //部门ID
    var deptid = document.getElementById("deptid").value;
    //岗位名称;
    var station = document.getElementById("station").value;
    // 动态表头、动态列
    var fieldnames = json.field.fieldnames;
    var fieldvalues = json.field.fieldvalues;

    var fieldnameArray = new Array();
    var fieldvalueArray = new Array();

    fieldnameArray = fieldnames.split(",");
    fieldvalueArray = fieldvalues.split(",");

    var fieldnameHtml = ""; // 表头
    for (i = 0; i < fieldnameArray.length; i++) {
        fieldnameHtml += "<td height='25' bgcolor='d3eaef' class='STYLE6'>";
        fieldnameHtml += fieldnameArray[i];
        fieldnameHtml += "</td>";
    }
    var fieldslength = fieldvalueArray.length;      // 字段总数

    var arr = [];
    arr.push("<table id='table1' width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<input type=\"checkbox\" name=\"checkboxAll\" id=\"checkboxAll\" onclick=\"cbSelectAll()\" />");
    arr.push("</td>");
    arr.push(fieldnameHtml);
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("类型");
    arr.push("</td>");
    arr.push("</tr>");

    var id_list = "";   // 保存查询出客源的ID 如 1211,1212,1213...
    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        id_list += record[i].id;
        if (i < recordLength - 1)
            id_list += ",";
            
        if (i % 2 == 0)
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#FFFFFF' style='cursor: pointer;' onclick=\"selectedTr('" + record[i].id + "')\" >");
        else
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#EEEDED' style='cursor: pointer;' onclick=\"selectedTr('" + record[i].id + "')\" >");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push("<input type='checkbox' name='checkbox_" + record[i].id + "' id='" + record[i].id + "' onclick=\"cbSelect(this, " + record[i].id + ")\" />");
        arr.push("</td>");

        if (fieldslength > 0) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field1);
            arr.push("</td>");
        }
        if (fieldslength > 1) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push("<a href='#' onclick=\"openDetail('" + record[i].id + "')\">" + record[i].field2 + "</a>");
            arr.push("</td>");
        }
        if (fieldslength > 2) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field3);
            arr.push("</td>");
        }
        if (fieldslength > 3) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field4);
            arr.push("</td>");
        }
        if (fieldslength > 4) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field5);
            arr.push("</td>");
        }
        if (fieldslength > 5) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field6);
            arr.push("</td>");
        }
        if (fieldslength > 6) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field7);
            arr.push("</td>");
        }
        if (fieldslength > 7) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field8);
            arr.push("</td>");
        }
        if (fieldslength > 8) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field9);
            arr.push("</td>");
        }
        if (fieldslength > 9) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field10);
            arr.push("</td>");
        }
        if (fieldslength > 10) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field11);
            arr.push("</td>");
        }
        if (fieldslength > 11) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field12);
            arr.push("</td>");
        }
        if (fieldslength > 12) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field13);
            arr.push("</td>");
        }
        if (fieldslength > 13) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field14);
            arr.push("</td>");
        }
        if (fieldslength > 14) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field15);
            arr.push("</td>");
        }
        if (fieldslength > 15) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field16);
            arr.push("</td>");
        }

        arr.push("<td height='20' class='STYLE6'>");
        if (record[i].isself == 0)
            arr.push("(私客)");
        else if (record[i].isself == 1)
            arr.push("(公客)");

        if (record[i].status == 2)
            arr.push("已成交");

        arr.push("</td>");
        arr.push("</tr>");
    }
    arr.push("</table>");
    $("#divList").html(arr.join('')); // 显示列表

    $("#divPage").html(""); // 清空
    //if (pageall > 1) {
        var pageHtml = LexunJS.showPage(currentPage, pageall, "showList"); // 分页
        $("#divPage").html(pageHtml);
    //}

    $("#hID_List").val(id_list);
}

// 响应键盘查询
function responseEnter(e) {
    var key = window.event ? e.keyCode : e.which;
    if (key == 13)
        search();
}

// 条件查询
function search() {
    $("#divList").html(""); // 清空列表
    $("#divPage").html(""); // 清空分页

    showList(1);
}

// 单选
function cbSelect(obj, objvalue) {
    if (obj.checked) {
        LexunJS.addOrCutData(1, "hID", objvalue);
    }
    else {
        LexunJS.addOrCutData(0, "hID", objvalue);
    }
}

// 全选
function cbSelectAll() {
    document.getElementById("hID").value = "";
    var cbAll = document.getElementById("checkboxAll");
    var checkList = document.getElementsByTagName("input");

    if (cbAll.checked) {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox") {
                checkList[i].checked = true;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(1, "hID", checkList[i].id);
            }
        }
    }
    else {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox") {
                checkList[i].checked = false;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(0, "hID", checkList[i].id);
            }
        }
    }
}

// 级联加载列表
function loadCity(sCity1, sCity2) {
    var parentid = document.getElementById(sCity1).value;
    if (parentid <= 0)
        return false;

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerIndex.ashx',
        data: 'act=area_list&parentid=' + parentid,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        }, success: function(json) {
            loadCity2CallBack(json, sCity2);
        }
    });
}

// 加载城市列表回调
function loadCity2CallBack(response, sCity2) {
    var json = eval("(" + response + ")");
    var record = json.record;

    $("#" + sCity2).html("");
    var obj = document.getElementById(sCity2);
    LexunJS.selectAddOption(obj, "全部", 0);

    if (record == null || record.length == 0)
        return;

    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        var sName = record[i].name;
        var sValue = record[i].id;

        LexunJS.selectAddOption(obj, sName, sValue);
    }
}

// 修改
function detail(id) {
    window.location.href = "Customer.aspx?id=" + id;
}

// 1转私客 2转公客
function changeTo(typeid) {
    var IDList = document.getElementById("hID").value;
    if (IDList == "") {
        alert("请先选择您要操作的行！");
        return false;
    }

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=customer_tranction_edit&typeid=' + typeid + '&id=' + IDList,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        }, success: function(json) {
            alert(json);
            showList(1);
        }
    });

    return false;
}

// 切换交易类型
function sTradeType_OnChange(selectName) {
    var sValue = LexunJS.GetSelectValue(selectName);
    if (sValue == "求租")
        $("#spanTradeTypeUnit").text("元/月");
    else
        $("#spanTradeTypeUnit").text("万元");
}

// 选中某行
function selectedTr(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");
}

// 查看详情
function openDetail(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");

    var id_list = $("#hID_List").val();
    var url = "../CM/CustomerDetail.aspx?tagNo=6&id=" + id + "&id_list=" + escape(id_list);

    var style = "left = 50, screenX = 0, top = 50, screenY = 0, resizable = yes, scrollbars = yes,width=750px, height=450px,Toolbar=no,Location=no,Direction=no,Resizeable=no";
    window.open(url, '', style);
    //self.parent.closeTab('客源详情');
    //self.parent.addTab('客源详情', url, 'icon-nav');
    //window.open("CustomerDetail.aspx?tagNo=6&id=" + id + "&id_list=" + escape(id_list), "target", LexunJS.openParams());
}        
